package com.zjj.wangluo.upload;

import java.io.*;
import java.net.Socket;

/**
 * @author zhoujunjie
 * @desc TODO
 * @date
 */
public class UploadClient {

    public static void main(String[] args) {
        try (
                //将文件从磁盘读入内存中
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\10307\\OneDrive\\Desktop\\IMG_20230112_130439.jpg"));
                //通过Socket写消息，写至服务端
                Socket socket = new Socket("127.0.0.1", 8888);
                BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());


                //读取服务器端回传的上传成功消息
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        ){
            //文件上传  边读边写
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes , 0 , len);
            }
            //写完刷新
            bos.flush();
            //写完socket停止输出
            socket.shutdownOutput();

            //读上传成功的消息
            System.out.println(br.readLine());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
